#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<queue>

 // Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> Q;
        vector<vector<int>> result;
        if(root==NULL){
            return result;
        }
        Q.push(root);
        int size = Q.size();
        while(!Q.empty()){
            vector<int> sonResurlt;
            for(int i = 0;i<size;++i){
                TreeNode* temp = Q.front();
                Q.pop();
                sonResurlt.push_back(temp->val);
                if(temp->left!=NULL)
                    Q.push(temp->left);
                if(temp->right!=NULL)
                    Q.push(temp->right);
            }
            result.push_back(sonResurlt);
            size = Q.size();
        }
        return result;        

    }
};


